package com.jrx.anytxn.param.service.product.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.product.PrPreterminationFeeTableReq;
import com.jrx.anytxn.param.dto.product.PrPreterminationFeeTableRes;
import com.jrx.anytxn.param.entity.PrPreterminationFeeTable;
import com.jrx.anytxn.param.mapper.ext.ExtPrPreterminationFeeTableMapper;
import com.jrx.anytxn.param.service.product.IPrPreterminationFeeTableService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 提前结清费表 业务接口实现
 *
 * @author yx
 * @date 2019/5/28
 */
@Service
@CacheConfig(cacheNames = "PRETERMINATION_FEE")
public class PrPreterminationFeeTableServiceImpl implements IPrPreterminationFeeTableService {
    private static final Logger logger = LoggerFactory.getLogger(PrPreterminationFeeTableServiceImpl.class);
    @Resource
    private ExtPrPreterminationFeeTableMapper extPrPreterminationFeeTableMapper;

    @Autowired
    private ISegmentService segmentService;


    @Override
    public TxnPage<PrPreterminationFeeTableRes> findAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<PrPreterminationFeeTable> page = PageHelper.startPage(pageNum, pageSize,Constant.ORDER_BY_SORT);
        List<PrPreterminationFeeTable> prPreterminationFeeTableList = extPrPreterminationFeeTableMapper.selectByTenId(tenantId);
        if (prPreterminationFeeTableList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PrPreterminationFeeTableRes> res = BeanMapping.copyList(prPreterminationFeeTableList, PrPreterminationFeeTableRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public PrPreterminationFeeTableRes addPreterminationFeeTable(PrPreterminationFeeTableReq prPreterminationFeeTableReq) throws TxnException {
        if (prPreterminationFeeTableReq.getMaxAmount() != null && prPreterminationFeeTableReq.getMinAmount() != null && prPreterminationFeeTableReq.getMaxAmount().compareTo(prPreterminationFeeTableReq.getMinAmount()) < 0) {
            throw new TxnBizException(TxnRespCode.BIZ_ERROR.getCode(), "输入有误,最大费用值要比最小费用值大");
        }
        PrPreterminationFeeTable prPreterminationFeeTable = extPrPreterminationFeeTableMapper.selectByPreterminationIdAndOrganizationId(prPreterminationFeeTableReq.getPreterminationFeeId(), prPreterminationFeeTableReq.getOrganizationId(), prPreterminationFeeTableReq.getTenantId());
        if (prPreterminationFeeTable != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PrPreterminationFeeTable preterminationFeeTable = BeanMapping.copy(prPreterminationFeeTableReq, PrPreterminationFeeTable.class);
        try {
            preterminationFeeTable.setId(segmentService.getId(Constant.PR_PRETERMINATION_FEE_TABLE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        preterminationFeeTable.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrPreterminationFeeTableMapper.insertSelective(preterminationFeeTable);
        return BeanMapping.copy(preterminationFeeTable, PrPreterminationFeeTableRes.class);
    }

    @CacheEvict(key = "'preterminationFee_'+#prPreterminationFeeTableReq.tenantId+'_'+#prPreterminationFeeTableReq.preterminationFeeId")
    @Override
    public PrPreterminationFeeTableRes modifyPreterminationFeeTable(PrPreterminationFeeTableReq prPreterminationFeeTableReq) throws TxnException {
        if (prPreterminationFeeTableReq.getMaxAmount() != null && prPreterminationFeeTableReq.getMinAmount() != null && prPreterminationFeeTableReq.getMaxAmount().compareTo(prPreterminationFeeTableReq.getMinAmount()) < 0) {
            throw new TxnBizException(TxnRespCode.BIZ_ERROR.getCode(), "输入有误,最大费用值要比最小费用值大");
        }
        PrPreterminationFeeTable prPreterminationFeeTable = extPrPreterminationFeeTableMapper.selectByTenIdAndFeeId(prPreterminationFeeTableReq.getTenantId(),prPreterminationFeeTableReq.getPreterminationFeeId());
        if (prPreterminationFeeTable == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }

        PrPreterminationFeeTable preterminationFeeTable = BeanMapping.copy(prPreterminationFeeTableReq, PrPreterminationFeeTable.class);
        // 更新
        extPrPreterminationFeeTableMapper.updateByTenIdAndFeeId(preterminationFeeTable);
        return BeanMapping.copy(preterminationFeeTable, PrPreterminationFeeTableRes.class);

    }

    @CacheEvict(key = "'preterminationFee_'+#tenantId+'_'+#feeId")
    @Override
    public Boolean removeByTenIdAndFeeId(String tenantId, String feeId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(feeId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrPreterminationFeeTableMapper.deleteByTenIdAndFeeId(tenantId, feeId);
        return i > 0;
    }

    @Override
    public PrPreterminationFeeTableRes findByTenIdAndFeeId(String tenantId, String feeId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(feeId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrPreterminationFeeTable preterminationFeeTable = extPrPreterminationFeeTableMapper.selectByTenIdAndFeeId(tenantId, feeId);
        return BeanMapping.copy(preterminationFeeTable, PrPreterminationFeeTableRes.class);
    }

    @Cacheable(key = "'preterminationFee_'+#tenantId+'_'+#preterminationFeeId")
    @Override
    public PrPreterminationFeeTableRes findByPreterminationFeeId(String preterminationFeeId, String orgId, String tenantId) throws TxnBizException {
        PrPreterminationFeeTable preterminationFeeTable = extPrPreterminationFeeTableMapper.selectByPreterminationIdAndOrganizationId(preterminationFeeId, orgId, tenantId);
        if (preterminationFeeTable == null) {
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode(), TxnRespCode.NO_DATA.getMsg(ErrorMsgConstant.PRETERMINATION_FEE));
        }
        return BeanMapping.copy(preterminationFeeTable, PrPreterminationFeeTableRes.class);
    }
}
